package com.lh.music.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lh.music.R;
import com.lh.music.adapter.NetSongAdapter;
import com.lh.music.api.Api;
import com.lh.music.callback.CallBack;
import com.lh.music.constant.Constants;
import com.lh.music.entity.NetSong;
import com.lh.music.util.RequestUtil;

import java.util.ArrayList;
import java.util.List;

public class DiantaiDetailActivity extends BaseActivity implements CallBack{
    private ListView listView;
    private TextView name;
    private List<NetSong> mData=new ArrayList<>();
    private NetSongAdapter adapter;
    private String channelName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_diantai_detail);
        listView= (ListView) findViewById(R.id.listView);
        name= (TextView) findViewById(R.id.channel_name);
        adapter=new NetSongAdapter(this,mData);
        listView.setAdapter(adapter);
        channelName=getIntent().getStringExtra("channelName");
        initData();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //发送play广播  更换当前播放列表
                app.currentNetSongList.clear();
                app.currentNetSongList.addAll(mData);
                app.currentPosition=position;
                Intent broadIntent=new Intent();
                broadIntent.setAction(Constants.NET_PLAY_ACTION);
                sendBroadcast(broadIntent);
            }
        });
    }

    private void initData() {
        RequestUtil requestUtil=new RequestUtil(this,this);
        requestUtil.getPaihangContent(Api.DIANTAI_DETAIL_API+channelName);
    }

    @Override
    public void callBack(String json) {
        JSONObject job= JSON.parseObject(json);
        JSONObject result=job.getJSONObject("result");
        //获取电台名字
        name.setText(result.getString("channel"));
        //获取该电台下的歌曲
        JSONArray array=result.getJSONArray("songlist");
        mData.addAll(JSON.parseArray(array.toJSONString(),NetSong.class));
        mData.remove(mData.size()-1);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void errorCallBack() {

    }

    @Override
    public View getChildContentView() {
        return getLayoutInflater().inflate(R.layout.activity_diantai_detail,null);
    }
}
